package carroejb;

import java.sql.Timestamp;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entidades.Carro;

/**
 * Session Bean implementation class CarroEJB.
 */
@Stateless(name = "CEJB", mappedName = "CEJB")
public class CarroEJB implements CarroEJBRemote {

    /**
     * Entity manager del EJB.
     */
    @PersistenceContext(unitName = "JPATINET")
    private EntityManager EM;

    /**
     * Default constructor.
     */
    public CarroEJB() {

    }

    /**
     * @see carroejb.CarroEJBRemote#addCarro()
     */
    public void addCarro(String username) {
        Carro c = new Carro();
        c.setUsuario(username);
        EM.persist(c);

    }

    /**
     * @see carroejb.CarroEJBRemote#AddLogin()
     */
    public void addLogin(int idCarro, String username) {
        Carro c = EM.find(Carro.class, idCarro);
        c.setUsuario(username);
        EM.merge(c);
    }

    /**
     * @see carroejb.CarroEJBRemote#RealizarCompra()
     */
    public void realizarCompra(int idCarro, Timestamp ts) {
        Carro c = EM.find(Carro.class, idCarro);
        c.setFechaCompra(ts);
    }

    /**
     * @see carroejb.CarroEJBRemote#getListCarro()
     */
    public List<Carro> getListCarro() {
        Query q = EM.createQuery("Select c from Carro c");
        @SuppressWarnings("unchecked")
        List<Carro> listC = (List<Carro>) q.getResultList();
        return listC;
    }

}
